#include <51func.h>
// 设计三个包装函数
void freep1(void *arg){
    printf("free p1\n");
    void *p1 = arg;
    free(p1);
}
void freep2(void *arg){
    printf("free p2\n");
    void *p2 = arg;
    free(p2);
}
void freep3(void *arg){
    // close(fd3);
    printf("free fd3\n");
    int * pfd3 = (int *)arg;
    close(*pfd3);
}
void *threadFunc(void *arg){
    void * p1 = malloc(1024);
    pthread_cleanup_push(freep1,p1); //cleanup_push 不是取消点
    // ...
    void * p2 = malloc(1024);
    pthread_cleanup_push(freep2,p2);
    // ...
    int fd3 = open("file1",O_RDWR);
    pthread_cleanup_push(freep3,&fd3);
    return NULL;
    // ......
    // close(fd3); --> pop
    pthread_cleanup_pop(1);
    // free(p2) --> pop
    pthread_cleanup_pop(1);
    // free(p1) --> pop
    pthread_cleanup_pop(1);
    printf("---------------------\n");
    pthread_exit(NULL);
}
int main(int argc, char *argv[])
{
    pthread_t tid;
    pthread_create(&tid,NULL,threadFunc,NULL);
    pthread_join(tid,NULL);
    return 0;
}

